﻿if (typeof jQuery === "undefined") {
    throw new Error("whoever requires jQuery");
}

if (typeof ko === "undefined") {
    throw new Error("whoever requires ko");
}

var whoever = (function ($, _, ko) {
    "use strict";

    var _observables = (function () {

        return {
            GetBy: function (list, key, value) {
                return $.grep(list, function (item) {
                    if (item[key]() == value)
                        return item;
                });
            }
        };
    })();

    var _ajax = (function () {

        return {
            Method: function (options) {
                var data = options.JSONdata ? options.JSONdata : ((options.data) ? options.data : {});
                var action = options.action ? options.action : (options.formId ? $("#" + options.formId).attr("action") : "");
                var method = options.method ? options.method : (options.formId ? $("#" + options.formId).attr("method") : "POST");
                var async = options.async ? options.async : false;
                var headers = {};
                
                headers['cache-control'] = "no-cache";
                headers['__RequestVerificationToken'] = $('#'+ options.formId + ' input[name="__RequestVerificationToken"]').val();

                var request = $.ajax({
                    url: action,
                    type: method,
                    data: data,
                    headers: headers,
                    cache: false,
                    traditional: true,
                    async: async,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json"
                });

                return request;
            }
        };
    })();

    var _validation = (function () {
        return {
            IsEmpty: function (value) {
                return ($.trim(value) ? false : true);
            },
            EmailIsValid: function (value) {
                var sQtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
                var sDtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
                var sAtom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
                var sQuotedPair = '\\x5c[\\x00-\\x7f]';
                var sDomainLiteral = '\\x5b(' + sDtext + '|' + sQuotedPair + ')*\\x5d';
                var sQuotedString = '\\x22(' + sQtext + '|' + sQuotedPair + ')*\\x22';
                var sDomain_ref = sAtom;
                var sSubDomain = '(' + sDomain_ref + '|' + sDomainLiteral + ')';
                var sWord = '(' + sAtom + '|' + sQuotedString + ')';
                var sDomain = sSubDomain + '(\\x2e' + sSubDomain + ')*';
                var sLocalPart = sWord + '(\\x2e' + sWord + ')*';
                var sAddrSpec = sLocalPart + '\\x40' + sDomain; // complete RFC822 email address spec
                var sValidEmail = '^' + sAddrSpec + '$'; // as whole string

                var reValidEmail = new RegExp(sValidEmail);

                if (value && value !== "")
                    if (reValidEmail.test(value))
                        return true;
                    else
                        return false;
                else
                    return true;
            }
        };
    })();

    var _animation = (function () {
        var _main = function () {
            var main = $(".main");
            if (!main) console.error("Container with class main must be added");
            return main;
        };

        return {
            MainExitLeft: function () {
                var newLeft = $(window).width() * -1;
                var move = $.when(_main().stop().animate({ left: newLeft }));

                return move;
            },
            MainExitRight: function () {
                var newLeft = $(window).width();
                var move = $.when(_main().stop().animate({ left: newLeft }));

                return move;
            },
            MainEnter: function () {
                var move = $.when(_main().stop().animate({ left: 0 }));

                return move;
            },
            MainHidden: function () {
                _main().css("display", "none");
            },
            MainVisible: function () {
                _main().css("display", "");
            },
            MainRight: function () {
                var newLeft = $(window).width();
                _main().css("left", newLeft);
            },
            MainLeft: function () {
                var newLeft = $(window).width() * -1;
                _main().css("left", newLeft);
            }
        };
    })();

    var _form = (function () {
        return {
            DisableAll: function () {
                $("input").attr("disabled", "disabled");
                $("select").attr("disabled", "disabled");
                $("button").attr("disabled", "disabled");
            },
            EnableAll: function () {
                $("input").removeAttr("disabled");
                $("select").removeAttr("disabled");
                $("button").removeAttr("disabled");
            },
            DisableButtons: function () {
                $("button").attr("disabled", "disabled");
            },
            EnableButtons: function () {
                $("button").removeAttr("disabled");
            },
            DisableActions: function () {
                $(".action").attr("disabled", "disabled");
            },
            EnableActions: function () {
                $(".action").removeAttr("disabled");
            },
            DisableBack: function () {
                $(".action-back").attr("disabled", "disabled");
            },
            EnableBack: function() {
                $(".action-back").removeAttr("disabled");
            }
        };
    })();

    return {
        Observables: _observables,
        AJAX: _ajax,
        Validation: _validation,
        Animation: _animation,
        Form: _form
    };
})(jQuery, _, ko);
